﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProduceBasicData.Api.Application.Command.DeviceManage;
using ProduceBasicData.Api.Application.Command.RBAC.Department;
using ProduceBasicData.Api.DTO.RBACDto;
using ProduceBasicData.ErrorCode;

namespace ProduceBasicData.Api.Controllers.RBAC
{
    /// <summary>
    /// 部门控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DepartmentsController : ControllerBase
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public DepartmentsController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateDepartment(CreateDepartmentCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<DepartmentsDto>> GetDepartmentList([FromQuery] GetDepartmentCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 获取所有部门列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<DepartmentsDto>>> GetAllDepartmentList([FromQuery] GetAllDepartmentCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 判断是否有子部门
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<bool>> HasChildDepartment([FromQuery] HasChildDepartmentCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateDepartment(UpdateDepartmentCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DeleteDepartment(DeleteDepartmentCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 部门批量删除接口
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DeleteRangeDepartment(DeleteRangeDepartmentCommand command)
        {
            return await _mediator.Send(command);
        }
    }
}
